<?php

namespace app\admin\controller;

use app\admin\common\Base;
use think\Db;
use think\Session;

class User extends Base
{
    public function index()
    {

        $data = $this->request->param();
        if (isset($data['ajax']) && $data['ajax'] == 1) {
            $map = '';
            //$map=$this->search($data);
            $page = isset($data['page']) ? $data['page'] : 1;
            $page = intval($page);
            $limit = isset($data['rows']) ? $data['rows'] : 10;
            $limit = intval($limit);
            $start = $limit * ($page - 1);

            if ($map == '') {
                $list = Db::name('admin')->order('sort desc,id desc')->limit($start, $limit)->select();
                $count = Db::name('admin')->order('sort desc,id desc')->count();
            } else {
                $list = Db::name('admin')->order('sort desc,id desc')->limit($start, $limit)->select();
                $count = Db::name('admin')->order('sort desc,id desc')->count();
            }
            foreach ($list as $key => &$val) {
                $val['addtime'] = date('Y-m-d', $val['addtime']);
                $val['company_name'] = Db::name('company')->where('id', $val['company'])->value('name');
                $val['group'] = Db::name('admin_group')->where('id', $val['group_id'])->value('name');
                $val['kf'] = '否';
                if ($val['is_kf'] == 1) {
                    $val['kf'] = '是';
                }

                 $val['wx'] = '否';
                if ($val['is_wx'] == 1) {
                    $val['wx'] = '是';
                }
            }
            return $this->json_data($count, $list);
        }
        return $this->fetch();
    }

    //编辑栏目1-1
    public function add($id)
    {
        if (empty($id)) {
            $info = [];
        } else {
            $info = Db::name('admin')->where('id', $id)->find();
        }
        $company = Db::name('company')->order('id desc')->select();
        $this->assign('company', $company);
        $cate = Db::name('admin_group')->order('sort desc ,id desc')->select();
        $this->assign('cate', $cate);
        $this->assign('info', $info);
        return $this->fetch('add');
    }
    //编辑栏目1-2
    public function save()
    {
        $status = 0;
        $msg = "操作失败";
        $data = $this->request->param();
        if (empty($data['username'])) {
            $msg = "用户名不能为空";
            return ['status' => $status, 'msg' => $msg];
        }

        if (empty($data['id'])) {
            if (empty($data['password'])) {
                $msg = "密码不能为空";
                return ['status' => $status, 'msg' => $msg];
            }
            $password = md5($data['password']);
            $update = [
                'username' => $data['username'],
                'realname' => $data['realname'],
                 'company' => $data['company'],
                'is_kf'    => $data['is_kf'],
                 'is_wx'    => $data['is_wx'],
                'email'    => $data['email'],
                'telephone' => $data['telephone'],
                'password' => $password,
                'group_id' => $data['group_id'],
                'sort' => $data['sort'],
                'last_login_time' => time(),
                'addtime' => time(),
                'updatetime' => time(),
            ];
            //执行操作
            $res = Db::name('admin')->insert($update);
            if ($res) {
                adminlog('添加管理员', '添加');
                $status = 1;
                $msg = "更新成功";
            }
        } else {
            $id = $data['id'];

            $update = [
                'username' => $data['username'],
                'realname' => $data['realname'],
                 'company' => $data['company'],
                'group_id' => $data['group_id'],
                'is_kf'    => $data['is_kf'],
                 'is_wx'    => $data['is_wx'],
                'email'    => $data['email'],
                'telephone' => $data['telephone'],
                'sort' => $data['sort'],
                'updatetime' => time(),
            ];
            //执行更新操作
            $res = Db::name('admin')->where('id', $id)->update($update);
            if ($res) {
                $user_id = Session::get('user_id');
                Session::set('user_info', Db::name('admin')->where('id', $user_id)->find());
                adminlog('修改管理员', '修改');
                $status = 1;
                $msg = "更新成功";
            }
        }
        return ['status' => $status, 'msg' => $msg];
    }


    //删除栏目1-1
    public function del($id)
    {
        $status = 0;
        $msg = "操作失败";
        if (empty($id)) {
            $info = [];
        } else {
            $del = Db::name('admin')->where('id', $id)->delete();
            if ($del) {
                adminlog('删除管理员', '删除');
                $status = 1;
                $msg = "删除成功";
            }
        }
        return ['status' => $status, 'msg' => $msg];
    }

    //修改密码1-1
    public function pwd($id)
    {

        $info = Db::name('admin')->where('id', $id)->find();
        $this->assign('info', $info);
        return $this->fetch('pwd');
    }
    //修改密码1-2
    public function savepwd()
    {
        $status = 0;
        $msg = "操作失败";
        $data = $this->request->param();

        if (empty($data['password2']) || empty($data['password3'])) {
            $msg = "新密码不能为空";
            return ['status' => $status, 'msg' => $msg];
        }
        if ($data['password2'] != $data['password3']) {
            $msg = "两次新密码不一致";
            return ['status' => $status, 'msg' => $msg];
        }
        $id = $data['id'];
        $pwd = md5(trim($data['password2']));
        $update = [
            'password' => $pwd,
            'updatetime' => time(),
        ];
        //执行更新操作
        $res = Db::name('admin')->where('id', $id)->update($update);
        if ($res) {
            adminlog('修改密码', '修改');
            $status = 1;
            $msg = "更新成功";
        }
        return ['status' => $status, 'msg' => $msg];
    }

    public function auth()
    {
        $this->assign('id', input('id'));
        return $this->fetch();
    }

    // 获取所有菜单
    public function getmenus()
    {
        $user_id = input('id');
        $userinfo = Db::name('admin')->where('id', $user_id)->find();
        if ($userinfo['auth'] != 'all') {
            $userinfo['auth'] = explode(',', $userinfo['auth']);
            $power = Db::name('power')->where('admin_id', $user_id)->find();
            // $userinfo['operate'] = explode(',', $userinfo['operate']);
            $userinfo['add'] = explode(',', $power['add']);
            $userinfo['edit'] = explode(',', $power['edit']);
            $userinfo['del'] = explode(',', $power['del']);
            $userinfo['createlabel'] = explode(',', $power['createlabel']);
            $userinfo['export'] = explode(',', $power['export']);
            $userinfo['downloadlabel'] = explode(',', $power['downloadlabel']);
            $userinfo['snimport'] = explode(',', $power['snimport']);
            $userinfo['delay'] = explode(',', $power['delay']);
            $userinfo['addcard'] = explode(',', $power['addcard']);
            $userinfo['activate'] = explode(',', $power['activate']);
            $userinfo['fix'] = explode(',', $power['fix']);
        }

        $list = Db::name('menu')->where(['status' => 1, 'pid' => 0])->order('sort desc,id desc')->select();
        foreach ($list as $key => &$val) {
            $val['text'] = $val['node_name'];
            $val['state'] = 0;
            $val['LookPower'] = false;
            $val['addPower'] = false;
            $val['editPower'] = false;
            $val['delPower'] = false;
            $val['createlabelPower'] = false;
            $val['exportPower'] = false;
            $val['downloadlabelPower'] = false;
            $val['snimportPower'] = false;
            $val['delayPower'] = false;
            $val['addcardPower'] = false;
            $val['activatePower'] = false;
            $val['fixPower'] = false;
            $vv=Db::name('power_v')->where(['menu_id' => $val['id']]) ->find();
            
            $val['addPower_v'] = $vv['add'];
            $val['editPower_v'] = $vv['edit'];
            $val['delPower_v'] =$vv['del'];
            $val['createlabelPower_v'] = $vv['createlabel'];
            $val['exportPower_v'] = $vv['export'];
            $val['downloadlabelPower_v'] = $vv['downloadlabel'];
            $val['snimportPower_v'] = $vv['snimport'];
            $val['delayPower_v'] = $vv['delay'];
            $val['addcardPower_v'] = $vv['addcard'];
            $val['activatePower_v'] = $vv['activate'];
            $val['fixPower_v'] = $vv['fix'];
            


            if ($userinfo['auth'] == 'all') {
                $val['LookPower'] = true;
                $val['addPower'] = true;
                $val['editPower'] = true;
                $val['delPower'] = true;
                $val['createlabelPower'] = true;
                $val['exportPower'] = true;
                $val['downloadlabelPower'] = true;
                $val['snimportPower'] = true;
                $val['delayPower'] = true;
                $val['addcardPower'] = true;
                $val['activatePower'] = true;
                $val['fixPower'] = true;
            } else {
                if (in_array($val['id'], $userinfo['auth'])) {
                    $val['LookPower'] = true;
                }
                if (!empty($userinfo['add']) && in_array($val['id'], $userinfo['add'])) {
                    $val['addPower'] = true;
                }
                if (!empty($userinfo['edit']) && in_array($val['id'], $userinfo['edit'])) {
                    $val['editPower'] = true;
                }
                if (!empty($userinfo['del']) && in_array($val['id'], $userinfo['del'])) {
                    $val['delPower'] = true;
                }
                if (!empty($userinfo['createlabel']) && in_array($val['id'], $userinfo['createlabel'])) {
                    $val['createlabelPower'] = true;
                }
                if (!empty($userinfo['export']) && in_array($val['id'], $userinfo['export'])) {
                    $val['exportPower'] = true;
                }
                if (!empty($userinfo['downloadlabel']) && in_array($val['id'], $userinfo['downloadlabel'])) {
                    $val['downloadlabel'] = true;
                }
                if (!empty($userinfo['snimport']) && in_array($val['id'], $userinfo['snimport'])) {
                    $val['snimport'] = true;
                }
                if (!empty($userinfo['delay']) && in_array($val['id'], $userinfo['delay'])) {
                    $val['delayPower'] = true;
                }
                if (!empty($userinfo['addcard']) && in_array($val['id'], $userinfo['addcard'])) {
                    $val['addcardPower'] = true;
                }
                if (!empty($userinfo['activate']) && in_array($val['id'], $userinfo['activate'])) {
                    $val['activatePower'] = true;
                }
                if (!empty($userinfo['fix']) && in_array($val['id'], $userinfo['fix'])) {
                    $val['fixPower'] = true;
                }
            }

            $children = Db::name('menu')->where('status', 1)->where('pid', $val['id'])->order('sort desc,id desc')->select();
            if (empty($children)) {
                $val['children'] = [];
            } else {
                foreach ($children as $k => &$v) {

                    $m=Db::name('power_v')->where(['menu_id' => $v['id']]) ->find();
            
                    $v['addPower_v'] = $m['add'];
                    $v['editPower_v'] = $m['edit'];
                    $v['delPower_v'] =$m['del'];
                    $v['createlabelPower_v'] = $m['createlabel'];
                    $v['exportPower_v'] = $m['export'];
                    $v['downloadlabelPower_v'] = $m['downloadlabel'];
                    $v['snimportPower_v'] = $m['snimport'];
                    $v['delayPower_v'] = $m['delay'];
                    $v['addcardPower_v'] = $m['addcard'];
                    $v['activatePower_v'] = $m['activate'];
                    $v['fixPower_v'] = $m['fix'];

                    $v['text'] = $v['node_name'];
                    $v['state'] = 0;
                    $v['LookPower'] = false;
                    $v['addPower'] = false;
                    $v['editPower'] = false;
                    $v['delPower'] = false;
                    $v['createlabelPower'] = false;
                    $v['exportPower'] = false;
                    $v['downloadlabelPower'] = false;
                    $v['snimportPower'] = false;
                    $v['delayPower'] = false;
                    $v['addcardPower'] = false;
                    $v['activatePower'] = false;
                    $v['fixPower'] = false;
                    if ($userinfo['auth'] == 'all') {
                        $v['LookPower'] = true;
                        $v['addPower'] = true;
                        $v['editPower'] = true;
                        $v['delPower'] = true;
                        $v['createlabelPower'] = true;
                        $v['exportPower'] = true;
                        $v['downloadlabelPower'] = true;
                        $v['snimportPower'] = true;
                        $v['delayPower'] = true;
                        $v['addcardPower'] = true;
                        $v['activatePower'] = true;
                        $v['fixPower'] = true;
                    } else {
                        if (in_array($v['id'], $userinfo['auth'])) {
                            $v['LookPower'] = true;
                        }
                        if (!empty($userinfo['add']) && in_array($v['id'], $userinfo['add'])) {
                            $v['addPower'] = true;
                        }
                        if (!empty($userinfo['edit']) && in_array($v['id'], $userinfo['edit'])) {
                            $v['editPower'] = true;
                        }
                        if (!empty($userinfo['del']) && in_array($v['id'], $userinfo['del'])) {
                            $v['delPower'] = true;
                        }
                        if (!empty($userinfo['createlabel']) && in_array($v['id'], $userinfo['createlabel'])) {
                            $v['createlabelPower'] = true;
                        }
                        if (!empty($userinfo['export']) && in_array($v['id'], $userinfo['export'])) {
                            $v['exportPower'] = true;
                        }
                        if (!empty($userinfo['downloadlabel']) && in_array($v['id'], $userinfo['downloadlabel'])) {
                            $v['downloadlabelPower'] = true;
                        }
                        if (!empty($userinfo['snimport']) && in_array($v['id'], $userinfo['snimport'])) {
                            $v['snimportPower'] = true;
                        }
                        if (!empty($userinfo['delay']) && in_array($v['id'], $userinfo['delay'])) {
                            $v['delayPower'] = true;
                        }
                        if (!empty($userinfo['addcard']) && in_array($v['id'], $userinfo['addcard'])) {
                            $v['addcardPower'] = true;
                        }
                        if (!empty($userinfo['activate']) && in_array($v['id'], $userinfo['activate'])) {
                            $v['activatePower'] = true;
                        }
                        if (!empty($userinfo['fix']) && in_array($v['id'], $userinfo['fix'])) {
                            $v['fixPower'] = true;
                        }
                    }
                }
                $val['children'] = $children;
            }
        }
        // halt($list);
        return json($list);
    }

    // 更新权限
    public function save_auth()
    {
        $status = 0;
        $msg = "操作失败";
        $data = $this->request->param();

        $user = Db::name('admin')->where('id', $data['id'])->find();
        if ($user['auth'] == 'all') {
            return ['status' => 1, 'msg' => '操作成功'];
        }
        $data['auth'] = empty($data['auth']) ? '' : implode(',', $data['auth']);

        $power['admin_id'] = $data['id'];
        $power['add'] = empty($data['add']) ? '' : implode(',', $data['add']);
        $power['edit'] = empty($data['edit']) ? '' : implode(',', $data['edit']);
        $power['del'] = empty($data['del']) ? '' : implode(',', $data['del']);
        $power['createlabel'] = empty($data['createlabel']) ? '' : implode(',', $data['createlabel']);
        $power['export'] = empty($data['export']) ? '' : implode(',', $data['export']);
        $power['downloadlabel'] = empty($data['downloadlabel']) ? '' : implode(',', $data['downloadlabel']);
        $power['snimport'] = empty($data['snimport']) ? '' : implode(',', $data['snimport']);
        $power['delay'] = empty($data['delay']) ? '' : implode(',', $data['delay']);
        $power['addcard'] = empty($data['addcard']) ? '' : implode(',', $data['addcard']);
        $power['activate'] = empty($data['activate']) ? '' : implode(',', $data['activate']);
        $power['fix'] = empty($data['fix']) ? '' : implode(',', $data['fix']);

        $rest = Db::name('admin')->where('id', $data['id'])->update(['auth' => $data['auth']]);
        
        $res = Db::name('power')->where('admin_id', $power['admin_id'])->find();
        if ($res) {
            $res2 = Db::name('power')->where('admin_id', $power['admin_id'])->update($power);
        } else {
            $res2 =  Db::name('power')->insert($power);
        }
        if ($res2) {
            adminlog('分配' . $user['username'] . '权限', '修改');
            return ['status' => 1, 'msg' => '操作成功'];
        }
        return ['status' => $status, 'msg' => $msg];
    }
}
